package com.huafeng.answer;


import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.huafeng.api.OpenAiApi;
import com.huafeng.config.OpenAiConfig;
import com.huafeng.entity.openai.Request;
import com.huafeng.entity.openai.Response;
import com.huafeng.utils.SpringContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 花风
 * @date 2023/4/12 12:07
 * @Description openAi回复
 */
@Service
@Slf4j
@com.huafeng.annotations.Answer(type = "openai")
public class OpenAiAnswer implements Answer{

    @Override
    public String createAnswer(String message) {
        Response response = OpenAiApi.doAnswer(message);
        List<Response.ChoicesItem> choicesItemList = response.getChoices();
        String answer = choicesItemList.stream()
                .map(Response.ChoicesItem::getText)
                .collect(Collectors.joining());
        log.info("OpenAiAnswer 回答成功 \n 答案：{}",answer);
        return answer;
    }


}
